﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using Kyoh.Lib.Plugin;

namespace Kyoh.App.Crawler.Plugin
{
    public abstract class TypicalRedirect : MarshalByRefObject, IRedirect
    {
        protected Regex SupportedRegex;

        protected TypicalRedirect(string supportedPattern)
        {
            SupportedRegex = new Regex(supportedPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }

        #region IRedirect メンバー

        public virtual bool Supports(string uri)
        {
            return SupportedRegex.IsMatch(uri);
        }

        public string Parse(string uri)
        {
            var req = (HttpWebRequest)WebRequest.Create(uri);
            req.AllowAutoRedirect = false;
            var res = req.GetResponse();
            return res.Headers[HttpResponseHeader.Location];
        }

        #endregion

        public abstract string Author { get; }
        public abstract string Description { get; }
        public abstract Uri Uri { get; }

        public virtual object Preferences { get { return null; } set { } }
        public abstract string Name { get; }

        public void Dispose() { Dispose(true); }
        ~TypicalRedirect() { Dispose(false); }
        protected void Dispose(bool disposing) { }
    }
}